﻿using System;
using System.Collections.Generic;
using System.Text;

using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geodatabase;

namespace shudiangis.Common
{
    public class UserGeometryGenerate
    {
        /// <summary>
        /// 生成曲线
        /// </summary>
        /// <param name="pntStart"></param>
        /// <param name="pntEnd"></param>
        /// <returns></returns>
        public static IPolyline CreatePolyline(IPoint pntStart, IPoint pntEnd)
        {
            IPolyline polyline = new PolylineClass();

            polyline.FromPoint = pntStart;
            polyline.ToPoint = pntEnd;

            return polyline;
        }

        /// <summary>
        /// 生成多边形
        /// </summary>
        /// <param name="pntStart"></param>
        /// <param name="pntEnd"></param>
        /// <returns></returns>
        public static IPolygon CreatePolygon(IPoint pntStart, IPoint pntEnd)
        {
            IPolygon polygon = new PolygonClass();

            polygon.FromPoint = pntStart;
            polygon.ToPoint = pntEnd;

            return polygon;
        }

        /// <summary>
        /// 生成直线
        /// </summary>
        /// <param name="pntStart"></param>
        /// <param name="pntEnd"></param>
        /// <returns></returns>
        public static ILine CreateLine(IPoint pntStart, IPoint pntEnd)
        {
            ILine line = new LineClass();

            line.FromPoint = pntStart;
            line.ToPoint = pntEnd;

            return line;
        }

        /// <summary>
        /// 设置画图的前提，橡皮条跟踪
        /// </summary>
        /// <param name="pActiveView"></param>
        /// <param name="pGeometry"></param>
        /// <returns></returns>
        public static IScreenDisplay InitSceenDisplay(IActiveView pActiveView, IGeometry pGeometry)
        {
            IScreenDisplay pScreenDisplay;//屏幕显示
            IRubberBand pRubberPolygon;//橡皮条跟踪
            ISimpleFillSymbol pFillSymbol;//设置样式
            IRgbColor pRgbColor;//颜色

            //Draw Polygon 
            pScreenDisplay = pActiveView.ScreenDisplay;

            pFillSymbol = new SimpleFillSymbol();
            pRgbColor = new RgbColor();
            pRgbColor.NullColor = true;
            pFillSymbol.Color = pRgbColor;
            
            pRubberPolygon = new RubberPolygon();
            pRubberPolygon.TrackExisting(pScreenDisplay, pFillSymbol as ISymbol,pGeometry);
            
            return pScreenDisplay;
        }

        /// <summary>
        /// 屏幕画图
        /// </summary>
        /// <param name="pScreenDisplay"></param>
        /// <param name="pGeometry"></param>
        public static void DrawGeometry(IScreenDisplay pScreenDisplay,IGeometry pGeometry)
        {
            ISimpleFillSymbol pFillSymbol;//设置样式
            IRgbColor pRgbColor;//颜色

            //Draw Polygon 
            pFillSymbol = new SimpleFillSymbol();
            pRgbColor = new RgbColor();
            pRgbColor.NullColor = true;
            pFillSymbol.Color = pRgbColor;

            pScreenDisplay.StartDrawing(pScreenDisplay.hDC, 0);
            pScreenDisplay.SetSymbol(pFillSymbol as ISymbol);
            pScreenDisplay.DrawPolygon(pGeometry);
            pScreenDisplay.FinishDrawing();
        }

        /// <summary>
        /// 生成GEOMETRY
        /// </summary>
        /// <param name="pActiveView"></param>
        /// <returns></returns>
        public static IGeometry GetGeomtry(IActiveView pActiveView,IPoint pntStart)
        {
            IScreenDisplay pScreenDisplay;//屏幕显示
            IRubberBand pRubberPolygon;//橡皮条跟踪
            ISimpleFillSymbol pFillSymbol;//设置样式
            IRgbColor pRgbColor;//颜色

            //Draw Polygon 
            pScreenDisplay = pActiveView.ScreenDisplay;
            pRubberPolygon = new RubberPolygon();
            pFillSymbol = new SimpleFillSymbol();
            pRgbColor = new RgbColor();
            pRgbColor.NullColor = true;

            pFillSymbol.Color = pRgbColor;

            pScreenDisplay.PanMoveTo(pntStart);
            IGeometry pPolygon = pRubberPolygon.TrackNew(pScreenDisplay, pFillSymbol as ISymbol);

            pScreenDisplay.StartDrawing(pScreenDisplay.hDC, 0);
            pScreenDisplay.SetSymbol(pFillSymbol as ISymbol);
            pScreenDisplay.DrawPolygon(pPolygon);
            pScreenDisplay.FinishDrawing();

            return pPolygon;
        }
    }
}
